<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 16:53:56
 * @LastEditTime: 2023-09-15 10:53:04
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Http\Services\BaseData;

use App\Http\Services\Service;
use App\Repositories\SubjectRepository;

use Illuminate\Support\Facades\DB;

/**
 * @property SubjectRepository $repository
 */
class SubjectService extends Service
    {

    public function __construct(SubjectRepository $repository)
        {
        $this->repository = $repository;
        }

    public function list()
        {
        $data = $this->repository->condition(request())->get();
        return $data;
        }


    public function menulist()
        {
        $data = $this->repository->condition(request())->getMenuList();
        return $data;
        }
    protected function save($request)
        {
        $data                = $request->toArray();
        $data['category_id'] = substr($data['code'], 0, 1);
        if (!in_array($data['category_id'], ['1', '2', '3', '4', '5'])) {
            fail("科目编号无效！");
            }
        if ($this->repository->whereCode($data['code'])->where("id", "<>", $data['id'] ?? 0)->exists()) {
            fail("此科目编号已存在！");
            }
        $data['parent_id'] = substr($data['code'], 0, strlen($data['code']) - 2);
        if (strlen($data['parent_id']) < 4) {
            $data['parent_id'] = 0;
            }

        if ($data['parent_id'] !== 0) {
            $parentdata = $this->repository->whereId($data['parent_id'])->getOne();

            if (strpos($data['code'], $parentdata['code']) === false) {
                fail("父科目编号：{$parentdata['code']}，子科目编号：{$data['code']}，不是父子关系！");
                }
            $data['full_name'] = $this->setFullName($data['parent_id']);

            }
        //子科目核算方式一致
        $this->repository->where("code", "<>", $data['code'])->where("parent_id", "like", $data['code'] . "%")->get()->filter(function ($v) use ($data)
            {
            $v->accounting   = $data['accounting'];
            $v->needquantity = $data['needquantity'];
            $v->unit         = $data['unit'];
            $v->save();
            });

        $data['full_name'] .= $data['name'];
        $data['id']        = $data['code'];

        return $this->repository->autosave($data);
        }
    private function setFullName($code)
        {
        $length = strlen($code);
        if ($length == 0) {
            return '';
            }
        $fullname = '';
        if ($length != 4) {
            $fullname = $this->setFullName(substr($code, 0, $length - 2)) . '/';
            }
        return $fullname . $this->repository->whereCode($code)->value("name");

        }
    private function autoDeleteChildren($item)
        {
        return $item->filter(function ($child)
            {
            if ($child->children) {
                $this->autoDeleteChildren($child->children);
                }
            return $child->delete();
            });

        }
    protected function delete()
        {
        return $this->autoDeleteChildren($this->repository->whereIn("id", $this->getRequestIds())->with(['children'])->get());
        }
    }